package com.zc.app.sys.controller;

import com.zc.app.sys.dao.peer.SystemMenuPeer;
import com.zc.app.sys.dao.peer.SystemRolePeer;
import com.zc.app.sys.dao.peer.SystemUserPeer;
import com.zc.app.sys.service.SystemRoleService;
import com.zc.app.sys.service.SystemUserService;
import com.zc.framework.common.bean.AjaxResult;
import com.zc.framework.common.bean.MyPageInfo;
import com.zc.framework.system.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.InvocationTargetException;

/**
 * Created by zc on 2016/12/19.
 */
@Controller
@RequestMapping("/sys/role")
public class SystemRoleController extends BaseController {

    private static Logger logger = LogManager.getLogger(SystemRoleController.class);

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private SystemUserService systemUserService;



    @InitBinder({ "sysRoleQuery" })
    public void initBinderQuery(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysRoleQuery.");
    }

    @InitBinder({ "sysRole" })
    public void initBindersysRole(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysRole.");
    }


    @RequestMapping(value = "/role_list", method = {RequestMethod.GET})
    public ModelAndView index(SystemRolePeer rolePeer){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/role_list");
        return modelAndView;
    }

    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public MyPageInfo<SystemRolePeer> search(@ModelAttribute("sysRoleQuery")SystemRolePeer rolePeer) throws InvocationTargetException, IllegalAccessException {
        if(rolePeer==null){
            rolePeer = new SystemRolePeer();
        }
        MyPageInfo<SystemRolePeer> pageInfo = systemRoleService.queryPageRoles(rolePeer, rolePeer.getPage(),rolePeer.getRows());
        logger.info("pageInfo:"+pageInfo);
        return pageInfo;
    }

    @RequestMapping(value = "/edit", method = {RequestMethod.GET})
    public ModelAndView edit(@RequestParam(value = "pageType",required = false)String pageType,
                             @RequestParam(value = "roleId",required = false)Integer roleId){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/role_edit");
        SystemRolePeer rolePeer = new SystemRolePeer();
        if("edit".equals(pageType)
                && roleId!=null){
            rolePeer = systemRoleService.queyRoleById(roleId);
        }
        modelAndView.addObject("sysRole", rolePeer);
        modelAndView.addObject("pageType",pageType);
        return modelAndView;
    }

    @RequestMapping(value = "/view", method = {RequestMethod.GET})
    public ModelAndView view(@RequestParam(value = "pageType",required = false)String pageType,
                             @RequestParam(value = "roleId",required = false)Integer roleId){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/role_view");
        SystemRolePeer rolePeer = new SystemRolePeer();
        if("edit".equals(pageType)
                && roleId!=null){
            rolePeer = systemRoleService.queyRoleById(roleId);
        }
        modelAndView.addObject("sysRole", rolePeer);
        modelAndView.addObject("pageType",pageType);
        return modelAndView;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult login(@ModelAttribute("sysRole") SystemRolePeer rolePeer,
                            @RequestParam(value = "pageType",required = false)String pageType) {

        SystemUserPeer user = systemUserService.getCurrentUser();

        if("edit".equals(pageType)){
            if (rolePeer.getRoleId()==null) {
                return fail(false, "账号ID不能为空!");
            }
            rolePeer.setUpdateBy(user.getUserName());
            systemRoleService.updateRole(rolePeer);
            return success(true);
        }

        if (StringUtils.isEmpty(rolePeer.getRoleName())) {
            return fail(false, "名字不能为空!");
        }

        rolePeer.setCreateBy(user.getUserName());
        rolePeer.setUpdateBy(user.getUserName());
        systemRoleService.createRole(rolePeer);
        return success(true);
    }
}
